﻿Public Class FormInvoiceView

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New FormInvoiceAdd
        frm.ShowDialog()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Cmd.Begin()
        Try
            Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
            Dim id = row.Cells("InvoiceId").Value
            Dim storeId = Cmd.ExecuteScalar("SELECT StoreId FROM Invoice WHERE InvoiceId=" & id)
            Cmd.ExecuteNonQuery("UPDATE Invoice SET Active=0 WHERE InvoiceId=" & id)
            Dim dtInvoiceDetail As DataTable = Cmd.ExecuteDataTable("SELECT * FROM InvoiceDetail WHERE Active=1 AND  InvoiceId = " & id)
            For Each r As DataRow In dtInvoiceDetail.Rows
                Cmd.ExecuteNonQuery("UPDATE Stock SET Qty = Qty + " & r("Qty") & " WHERE ProductId=" & r("ProductId") & " AND StoreId = " & storeId)
                Cmd.ExecuteNonQuery("UPDATE InvoiceDetail SET Active=0 WHERE InvoiceDetailId=" & r("InvoiceDetailId"))
            Next

            Cmd.Commit()
            LoadData()

        Catch ex As Exception
            Cmd.Rollback()
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub FormInvoiceView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.dgv)
        LoadData()
    End Sub
    Public Sub LoadData()
        Dim sql As String = "select i.InvoiceId,i.InvoiceNo,i.InvoiceDate,i.Subtotal,i.Discount,i.Total,c.CustomerName,s.StoreName from Invoice i INNER JOIN Customer c on c.CustomerId=i.CustomerId INNER JOIN Store s on s.StoreId=i.StoreId WHERE i.Active=1  AND  i.InvoiceDate BETWEEN '" & Me.DateTimePicker1.Value.ToString("yyyy-MM-dd") & "' AND '" & Me.DateTimePicker2.Value.ToString("yyyy-MM-dd") & "'"
        Me.dgv.DataSource = Cmd.ExecuteDataTable(sql)
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        Cmd.Parameters("@search") = "%" & txtsearch.Text & "%"
        dgv.DataSource = Cmd.ExecuteDataTable("select i.InvoiceId,i.InvoiceNo,i.InvoiceDate,i.Subtotal,i.Discount,i.Total,c.CustomerName,s.StoreName from Invoice i INNER JOIN Customer c on c.CustomerId=i.CustomerId INNER JOIN Store s on s.StoreId=i.StoreId WHERE i.Active=1 AND UPPER(InvoiceNo) like UPPER(@search) ")
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        LoadData()
    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        LoadData()
    End Sub
End Class